package com.bootdo.wx.api;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UUIDUtils;
import com.bootdo.wx.domain.GoodsBrandDO;
import com.bootdo.wx.domain.GoodsSearchDO;
import com.bootdo.wx.domain.GoodsWarehouseDO;
import com.bootdo.wx.domain.MemberSearchDO;
import com.bootdo.wx.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2019/5/24.
 */


@Controller
@RequestMapping("/api/goods")
public class ApiGoods {

    @Autowired
    private GoodsGoodsService goodsGoodsService;
    @Autowired
    private GoodsClassService goodsClassService;
    @Autowired
    private GoodsProductService goodsProductService;
    @Autowired
    private GoodsSpikeService goodsSpikeService;
    @Autowired
    private OrderComentService orderComentService;
    @Autowired
    private GoodsWarehouseService goodsWarehouseService;
    @Autowired
    private GoodsBrandService goodsBrandService;
    @Autowired
    private MemberStockService memberStockService;
    @Autowired
    private GoodsSearchService goodsSearchService;
    @Autowired
    private MemberSearchService memberSearchService;

    /**
     *
     * @Title: findGoods
     * @Description: 搜索商品
     * @param map
     * @return R
     * @author Administrator
     * @date 2018年9月27日上午10:31:23
     *  type 0 视频 1文章 2商品
     */
    @RequestMapping("/findGoods")
    @ResponseBody
    public R findGoods(@RequestBody Map<String, Object> map) {
        if (!map.containsKey("name")
        ||!map.containsKey("order")

              ){
            return R.error("参数错误");
        }
        try {
            return goodsGoodsService.findGoods(map);
        } catch (Exception e) {
            return R.error("查询错误");
        }

    }

    /***
     * 获取小类和商品
     * @param params  classId 全部传-1
     * @return
     */
    @ResponseBody
    @PostMapping("/getSmallClassAndGoods")
    public R getSmallClassAndGoods(@RequestBody(required=false) Map<String, Object> params) {
        if (!params.containsKey("classId")
        ||!params.containsKey("name")
        ) {
            return R.error("缺少参数");
        }
        try {

            return goodsClassService.getSmallClassAndGoods(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return R.error("获取小类列表失败");
        }
    }

    /**
     *
     * @Title: classList
     * @Description: 一级分类
     * @param map
     * @return R
     * @author long
     * @date 2019年3月20日上午10:10:48
     */
    @ResponseBody
    @PostMapping("/class/list")
    public R classList(@RequestBody(required=false) Map<String, Object> map) {
        try {
            map.put("level","0");
            return goodsClassService.classList(map);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return R.error("获取小类列表失败");
        }
    }

    /**
     *
     * @Title: classList
     * @Description: 根据一级分类得到二级三级分类
     * @param params
     * @return R
     * @author long
     * @date 2019年3月20日上午10:32:02
     */
    @ResponseBody
    @PostMapping("/smallClassList")
    public R smallClassList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("parentId") || StringUtils.isBlank(params.get("parentId").toString())) {
            return R.error("缺少参数");
        }
        try {
            return  goodsClassService.queryByPrentId(params);
        } catch (Exception e) {
            return R.error("获取小类列表失败");
        }
    }

    /**
     *
     * @Title: classList
     * @Description: 三级分类
     * @param map
     * @return R
     * @author long
     * @date 2019年3月20日上午10:10:48
     */
    @ResponseBody
    @PostMapping("/allSmallClassList")
    public R allSmallClassList(@RequestBody(required=false) Map<String, Object> map) {
        try {
            map.put("level","2");
            return goodsClassService.classList(map);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return R.error("获取三级分类列表失败");
        }
    }

    /**
     *
     * @Title: goodsList
     * @Description: 根据小类、品牌、货仓,查询商品列表
     * @param params
     * @return R
     * @author long
     * @date 2019年3月23日上午11:07:44
     */
    @ResponseBody
    @PostMapping("/goodsListByClass")
    public R goodsListByClass(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("method") || StringUtils.isBlank(params.get("method").toString())
                || !params.containsKey("order")
                || !params.containsKey("userId")
                || !params.containsKey("classId")
               ) {
            return R.error("缺少参数");
        }
        try {
            return  goodsGoodsService.goodsListByClass(params);
        } catch (Exception e) {
            return R.error("获取小类列表失败");
        }
    }

    /**
     *
     * @Title: recommend
     * @Description: 推荐列表 type 0：首页新品，1：精品推送，2：新品放送 3:首页特价 4:最新活动 5:返券商品
     * @param params
     * @return R
     * @author long
     * @date 2019年3月23日上午9:47:58
     */
    @ResponseBody
    @PostMapping("/recommend")
        public R recommend(@RequestBody Map<String, Object> params) {
            if (!params.containsKey("type") || StringUtils.isBlank(params.get("type").toString())
                   ) {
                return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.recommend(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return R.error("获取失败");
        }
    }

    /**
     * 普通商品详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getDetail")
    public R getDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("goodsId") || StringUtils.isBlank(params.get("goodsId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 秒杀商品详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getSpikeDetail")
    public R getSpikeDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("spikeId") || StringUtils.isBlank(params.get("spikeId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getSpikeDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 9.9专区商品详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getNineDetail")
    public R getNineDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("nineId") || StringUtils.isBlank(params.get("nineId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getNineDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 砍价商品详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getBargainDetail")
    public R getBargainDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("bargainId") || StringUtils.isBlank(params.get("bargainId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getBargainDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 会员专区商品详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getUpgradeDetail")
    public R getUpgradeDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("upgradeId") || StringUtils.isBlank(params.get("upgradeId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getUpgradeDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 拼团专区商品详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getTeamDetail")
    public R getTeamDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("teamId") || StringUtils.isBlank(params.get("teamId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getTeamDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }


    /**
     * 根据商品ID和选择规格 获取产品价格
     * type 0: 普通商品， 1：秒杀商品, 2:9.9专区商品， 4：拼团商品，5：会员专区商品
     * id：根据type值传相应id
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/getProduct")
    public R getProduct(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("id") || StringUtils.isBlank(params.get("id").toString()) ||
                !params.containsKey("spec") || 
                !params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString()) ||
                !params.containsKey("type") || StringUtils.isBlank(params.get("type").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            Map<String,Object> goodsProductDO = goodsProductService.getByGoodsIdAndSpec(params);
            if (goodsProductDO==null){
                return R.error(101,"产品异常");
            }
            return R.ok().put("data", goodsProductDO);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("产品详情失败");
        }
    }


    /**
     * 查看评价
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/getGoodsComent")
    public R getGoodsComent(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("goodsId") || StringUtils.isBlank(params.get("goodsId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                ){
            return R.error("缺少参数");
        }
        try {
            PageInfo<Map<String,Object>> list = orderComentService.getComentByGoods(params);
            return R.ok().put("data",list);
            //return goodsGoodsService.getDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 货仓列表
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/getWarehouse")
    public R getWarehouse(@RequestBody Map<String, Object> params) {
        try {
            params.put("delFlag", "0");
            List<GoodsWarehouseDO> list = goodsWarehouseService.list(params);
            return R.ok().put("data",list);
            //return goodsGoodsService.getDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 品牌列表
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/getBrand")
    public R getBrand(@RequestBody Map<String, Object> params) {
        try {
            params.put("delFlag", "0");
            List<GoodsBrandDO> list = goodsBrandService.list(params);
            return R.ok().put("data",list);
            //return goodsGoodsService.getDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }
    /**
     * 库存列表
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/getMemberStockList")
    public R getMemberStockList(@RequestBody Map<String, Object> params) {
    	 if (
                 !params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                 ){
             return R.error("缺少参数");
         }
    	try {
    		params.put("flag", "0");
    		return memberStockService.getMemberStockList(params);
    		//return goodsGoodsService.getDetail(params);
    	}catch (Exception e){
    		e.printStackTrace();
    		return R.error("商品详情失败");
    	}
    }
    
    /**
     * 库存记录
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/getMemberStockRecordList")
    public R getMemberStockRecordList(@RequestBody Map<String, Object> params) {
    	 if (
                 !params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())||
                 !params.containsKey("goodsId") || StringUtils.isBlank(params.get("goodsId").toString())||
                 !params.containsKey("productId") || StringUtils.isBlank(params.get("productId").toString())
                 ){
             return R.error("缺少参数");
         }
    	try {
    		return memberStockService.getMemberStockRecordList(params);
    		//return goodsGoodsService.getDetail(params);
    	}catch (Exception e){
    		e.printStackTrace();
    		return R.error("商品详情失败");
    	}
    }

    /**
     * 搜索-热门搜索字段
     * @return
     */
    @ResponseBody
    @PostMapping("/searchHot")
    public R searchHot() {
        Map<String,Object>  query = new HashMap<>();
        query.put("type",0);
        query.put("del_flag",0);
        List<String> list2 = new ArrayList<>();
        try {
            List<MemberSearchDO> list = memberSearchService.list(query);
            for (MemberSearchDO MemberSearchDO : list) {
                list2.add(MemberSearchDO.getValue());
            }
            return R.ok().put("data",list2);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("获取热门搜索失败");
        }
    }

    /**
     * 搜索-热销榜列表
     * @return
     */
    @ResponseBody
    @PostMapping("/topList")
    public R topList(@RequestBody Map<String, Object> params) {
        try {
            PageInfo<Map<String,Object>> list =  goodsGoodsService.geToptList(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("获取热门搜索失败");
        }
    }

    /**
     * 普通商品详情-根据用户身份显示普通活动详情
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/goods/getGoodsDetail")
    public R getGoodsDetail(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("goodsId") || StringUtils.isBlank(params.get("goodsId").toString())
                ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
        ){
            return R.error("缺少参数");
        }
        try {
            return goodsGoodsService.getGoodsDetail(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("商品详情失败");
        }
    }

    /**
     * 普通商品详情-热销推荐
     * @return
     */
    @ResponseBody
    @PostMapping("/recommendGoodsList")
    public R recommendGoodsList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("status") || StringUtils.isBlank(params.get("status").toString())

        ){
            return R.error("缺少参数");
        }
        try {
            PageInfo<Map<String,Object>> list =  goodsGoodsService.recommendGoodsList(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("获取热门搜索失败");
        }
    }

    /**
     * 普通商品详情-售后须知
     * @return
     */
    @ResponseBody
    @PostMapping("/afterNotice")
    public R afternotice() {
        try {
            String afterNotice = goodsGoodsService.getAfterNotice();
            return R.ok().put("data", afterNotice);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取售后须知失败");
        }
    }

    /**
     *直播-正在直播列表
     * @param paramMap pageSize每页条数 pageNum当前页数
     * @return
     */
    @ResponseBody
    @RequestMapping("/getLiveList")
    public R getLiveList(@RequestBody Map<String,Object> paramMap) {
        try{
            PageInfo<Map<String,Object>> list = goodsGoodsService.getLiveGoods(paramMap);
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    /**
     *直播-预约直播列表
     * @param paramMap pageSize每页条数 pageNum当前页数
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAppointmentLiveList")
    public R getAppointmentLiveList(@RequestBody Map<String,Object> paramMap) {
        try{
            PageInfo<Map<String,Object>> list = goodsGoodsService.getAppointmentLiveList(paramMap);
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    /**
     *直播-预约直播按钮
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAppointmentLive")
    public R getAppointmentLive(@RequestBody Map<String,Object> param) {
        if (!param.containsKey("userId") || StringUtils.isBlank(param.get("userId").toString())
                || !param.containsKey("liveId") || StringUtils.isBlank(param.get("liveId").toString())){
            return R.error("参数错误");
        }

        try{
            return goodsGoodsService.addAppointmen(param);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }
}
